package com.bright.hxj.qchz.sql;

import cn.hutool.core.util.ArrayUtil;
import org.apache.ibatis.jdbc.SQL;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author txf
 * @date 2021/5/14 14:42
 * @Description
 */
public class SqlProvider {

    public String selectSql(String tableName, String columns, Map<String, Object> map, String orderBy){

        SQL sql = new SQL();
        sql.SELECT(columns);
        sql.FROM(tableName);
        List<String> sqlWhere = new ArrayList<>();
        for (String s : map.keySet()) {
            sqlWhere.add(""+ s +"=#{map."+ s +"}");
        }
        Object[] objects = sqlWhere.toArray();
        String[] strings = new String[objects.length];
        for (int i=0; i<objects.length; i++){
            strings[i] = objects[i].toString();
        }
        sql.WHERE(strings);
        //添加默认按排序号升序排序
        if (orderBy != null) {
            sql.ORDER_BY(orderBy);
        }
        return sql.toString();
    };

    public String insertSql(String tableName, List<Map<String, Object>> mapListData){
        StringBuilder sb = new StringBuilder();
//        List<Map<String, Object>> mapListData = (List<Map<String, Object>>) map.get("mapListData");
        sb.append("INSERT INTO " + tableName + " ");
        Set<String> keySet = null;
        for (int i = 0; i < mapListData.size(); i++){
            if(i == 0){
                keySet = mapListData.get(i).keySet();
            }
        }
        Object[] keyObject = keySet.toArray();
        String[] keyString = new String[keyObject.length];
        ArrayUtil.copy(keyObject, keyString, keyObject.length);
        String key = ArrayUtil.join(keyString, ",");
        sb.append("(" + key + ")");
        sb.append("VALUES ");

        int i = 0;
        for (Map<String, Object> mapData1 : mapListData) {
            List<Object> objectList = new ArrayList<>();
            for (String s : mapData1.keySet()) {
                objectList.add(mapData1.get(s));
            }
            String values = ArrayUtil.join(objectList.toArray(), ",");

            sb.append("(" + values + ")");
            if(i < mapListData.size() - 1){
                sb.append(",");
            }
            i++;
        }
      return sb.toString();
    };

    public String updateSql(String tableName, Map<String, Object> map){

        SQL sql = new SQL();
        sql.UPDATE(tableName);
        for (String s : map.keySet()) {
            if(!s.equalsIgnoreCase("id")){
                sql.SET(""+ s +"=#{map."+ s +"}");
            }
        }
        sql.WHERE("id=#{map.id}");

        System.out.println(sql.toString());
        return sql.toString();
    };

    public String deleteSqlByIds(String tableName, List<Integer> ids, String idColumnName){

        SQL sql = new SQL();
        sql.DELETE_FROM(tableName);
        String sqlWhere = "";
        for (int i = 0; i < ids.size(); i++){
            if(i < ids.size() - 1){
                sqlWhere += "#{ids["+ i +"]}, ";
            }else {
                sqlWhere += "#{ids["+ i +"]}";
            }
        }
        sql.WHERE(idColumnName + " in("+ sqlWhere +")");
        System.out.println(sql.toString());
        return sql.toString();
    }

    public String deleteSql(String tableName, Map<String, Object> map){

        SQL sql = new SQL();
        sql.DELETE_FROM(tableName);
        List<String> sqlWhere = new ArrayList<>();
        for (String s : map.keySet()) {
            sqlWhere.add(""+ s +"=#{map."+ s +"}");
        }
        Object[] objects = sqlWhere.toArray();
        String[] strings = new String[objects.length];
        for (int i=0; i<objects.length; i++){
            strings[i] = objects[i].toString();
        }
        sql.WHERE(strings);
        System.out.println(sql.toString());
        return sql.toString();
    }

    public String selectByFullSql(String sql, Map<String, Object> map) {
        return sql;
    }
}
